#include "Button.h"

N2F::Iw3D::Button::Button(const ScaleFactor &scaleFactor)
	: Element(scaleFactor)
{
	this->state = ButtonNormal;
	this->imgActive = NULL;
	this->imgNormal = NULL;
	this->imgDisabled = NULL;
	this->hitBox = NULL;

	return;
}

N2F::Iw3D::Button::~Button()
{
	return;
}

void N2F::Iw3D::Button::setNormalFile(const char *file)
{
	this->imgNormal = new Image(this->scaleFactor);
	this->imgNormal->setFile(file);

	return;
}

const char *N2F::Iw3D::Button::getNormalFile() const
{
	return(this->imgNormal->getFile());
}

void N2F::Iw3D::Button::setActiveFile(const char *file)
{
	this->imgActive = new Image(this->scaleFactor);
	this->imgActive->setFile(file);

	return;
}

const char *N2F::Iw3D::Button::getActiveFile() const
{
	return(this->imgActive->getFile());
}

void N2F::Iw3D::Button::setDisabledFile(const char *file)
{
	this->imgDisabled = new Image(this->scaleFactor);
	this->imgDisabled->setFile(file);

	return;
}

const char *N2F::Iw3D::Button::getDisabledFile() const
{
	return(this->imgDisabled->getFile());
}

void N2F::Iw3D::Button::setFiles(const char *normal, const char *active, const char *disabled)
{
	this->imgNormal = new Image(this->scaleFactor);
	this->imgNormal->setFile(normal);

	this->imgActive = new Image(this->scaleFactor);
	this->imgActive->setFile(active);

	this->imgDisabled = new Image(this->scaleFactor);
	this->imgDisabled->setFile(disabled);

	this->size = this->imgActive->getSize();

	return;
}

CIwFVec2 N2F::Iw3D::Button::getOffset()
{
	return(this->offset);
}

void N2F::Iw3D::Button::setOffset(CIwFVec2 offset)
{
	this->offset = offset;
}

float N2F::Iw3D::Button::getScale()
{
	return(this->scale);
}

void N2F::Iw3D::Button::setScale(float scale)
{
	this->scale = scale;
}

const N2FButtonState N2F::Iw3D::Button::getState() const
{
	return(this->state);
}

void N2F::Iw3D::Button::setState(const N2FButtonState state)
{
	this->state = state;

	return;
}

void N2F::Iw3D::Button::buildHitBox()
{
	this->size = this->imgActive->getSize();
	this->hitBox = new Rectangle(this->translatePosition(), this->translateSize());

	return;
}

void N2F::Iw3D::Button::OnClick(OnClickCallback callback, void *userData)
{
	this->clickCallback.callback = callback;
	this->clickCallback.userData = userData;

	return;
}

void N2F::Iw3D::Button::draw(int64 delta)
{
	switch (this->state)
	{
	case ButtonActive:
		this->imgActive->setPosition(this->pos);
		this->imgActive->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgActive->draw(delta);
		
		break;
	case ButtonDisabled:
		this->imgDisabled->setPosition(this->pos);
		this->imgDisabled->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgDisabled->draw(delta);

		break;
	case ButtonNormal:
		this->imgNormal->setPosition(this->pos);
		this->imgNormal->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgNormal->draw(delta);
		
		break;
	}

	return;
}

void N2F::Iw3D::Button::draw(int64 delta, CIwFVec2 offset)
{
	switch (this->state)
	{
	case ButtonActive:
		this->imgActive->setPosition(this->pos);
		this->imgActive->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgActive->draw(delta, offset);

		break;
	case ButtonDisabled:
		this->imgDisabled->setPosition(this->pos);
		this->imgDisabled->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgDisabled->draw(delta, offset);

		break;
	case ButtonNormal:
		this->imgNormal->setPosition(this->pos);
		this->imgNormal->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgNormal->draw(delta, offset);
		
		break;
	}

	return;
}

void N2F::Iw3D::Button::draw(int64 delta, CIwFVec2 offset, float angle)
{
	switch (this->state)
	{
	case ButtonActive:
		this->imgActive->setPosition(this->pos);
		this->imgActive->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgActive->draw(delta, offset, angle);

		break;
	case ButtonDisabled:
		this->imgDisabled->setPosition(this->pos);
		this->imgDisabled->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgDisabled->draw(delta, offset, angle);

		break;
	case ButtonNormal:
		this->imgNormal->setPosition(this->pos);
		this->imgNormal->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgNormal->draw(delta, offset, angle);
		
		break;
	}

	return;
}

void N2F::Iw3D::Button::draw(int64 delta, CIwFVec2 offset, float angle, float scale)
{
	this->offset = offset;
	this->scale = scale;

	switch (this->state)
	{
	case ButtonActive:
		this->imgActive->setPosition(this->pos);
		this->imgActive->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgActive->draw(delta, offset, angle, scale);

		break;
	case ButtonDisabled:
		this->imgDisabled->setPosition(this->pos);
		this->imgDisabled->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgDisabled->draw(delta, offset, angle, scale);

		break;
	case ButtonNormal:
		this->imgNormal->setPosition(this->pos);
		this->imgNormal->setPositioningMetrics(this->scaleGroup, this->originPoint);
		this->imgNormal->draw(delta, offset, angle, scale);
		
		break;
	}

	return;
}

void N2F::Iw3D::Button::OnKill()
{
	if (this->imgActive != NULL)
	{
		delete this->imgActive;
		this->imgActive = NULL;
	}

	if (this->imgDisabled != NULL)
	{
		delete this->imgDisabled;
		this->imgDisabled = NULL;
	}

	if (this->imgNormal != NULL)
	{
		delete this->imgNormal;
		this->imgNormal = NULL;
	}

	if (this->hitBox != NULL)
	{
		delete this->hitBox;
		this->hitBox = NULL;
	}

	return;
}

void N2F::Iw3D::Button::OnTouch(InputPoint point)
{
	if (!point.isMoving && !point.isPressed)
	{
		if (this->state == ButtonActive)
		{
			this->clickCallback.callback(&point, this->clickCallback.userData);
		}

		this->state = ButtonNormal;

		return;
	}

	if (point.isPressed && point.isDragging)
	{
		this->state = ButtonActive;

		return;
	}

	return;
}

bool N2F::Iw3D::Button::isHit(CIwFVec2 point)
{
	this->buildHitBox();

	if (this->hitBox->intersectedBy(point))
	{
		return(true);
	}

	return(false);
}
